/**
 * Created by huxia on 2016/12/9.
 */
var Doctor = require('./db')['doctor'];
var Department = require('./db')['department'];
var Hospital = require('./db')['hospital'];
var async = require('async');

Doctor.findById = function(doctor_id) {
	return Doctor.findOne({where:{id:doctor_id}});
};
/*
	通过医院的id和医生等级查找所有医生 */
Doctor.findByHospital = function (hp_id, d_level) {
	var dl = d_level;
	if (d_level < 1 || d_level > 4) {
		dl = [1, 2, 3, 4];
	}
	return Doctor.findAll({
		where: {
			'$and': [
				{hp_id: hp_id},
				{level: dl}
			]
		}
	});
};

/*
	通过医院id、医生所在一级科室查找所有该级别的医生 */
Doctor.findByHospitalFD = function (hp_id, f_id, d_level) {
	var dl = d_level;
	if (d_level < 1 || d_level > 4) {
		dl = [1, 2, 3, 4];
	}
	var s_id = [];
	s_id.push(f_id);
	return Department.findByPid(f_id).then(function (list_dept_s_f) {
		list_dept_s_f.forEach(function (item) {
			s_id.push(item.dataValues.id);
		});
		return Doctor.findAll({
			where:{
				'$and':[
					{hp_id:hp_id},
					{dpt_id:s_id},
					{level:dl}
				]
			}
		});
	});
};
/*
	通过医院id、医生所在二级科室查找所有该级别的医生 */
Doctor.findByHospitalSD = function (hp_id, s_id, d_level) {
	var dl = d_level;
	if (d_level < 1 || d_level > 4) {
		dl = [1, 2, 3, 4];
	}
	return Doctor.findAll({
		where:{
			'$and':[
				{hp_id:hp_id},
				{dpt_id:s_id},
				{level:dl}
			]
		}
	});
};

module.exports = Doctor;